package cn.sxt.logistics.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.sxt.logistics.pojo.MsgObject;
import cn.sxt.logistics.pojo.BaseData;
import cn.sxt.logistics.pojo.BaseDataExample;
import cn.sxt.logistics.pojo.BaseDataExample.Criteria;
import cn.sxt.logistics.pojo.User;
import cn.sxt.logistics.pojo.UserExample;
import cn.sxt.logistics.service.BaseDataService;
import cn.sxt.logistics.service.UserService;

@Controller
@RequestMapping("/baseData")
public class BaseDataController {

	@Autowired
	private BaseDataService baseDataService;
	

	@RequestMapping("/baseDataPage")
	@RequiresPermissions("baseData:baseDataPage")
	public String baseDataPage() {

		return "baseDataPage";
	}

	// 编辑页面，新增和修改公用的页面
	@RequestMapping("/edit")
	public String edit(Model m, Long baseId) {
		
		
		//查询出所有基础数据中 parent_id = null 类别数据
		BaseDataExample example = new BaseDataExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdIsNull();
		List<BaseData> parents = baseDataService.selectByExample(example );
		m.addAttribute("parents", parents);
		

		// 修改操作
		if (baseId != null) {
			// 查询出当前用户的数据，共享给编辑页面，以供修改的回显数据
			BaseData baseData = baseDataService.selectByPrimaryKey(baseId);
			m.addAttribute("baseData", baseData);
		}

		return "baseDataEdit";
	}
	
	@RequiresPermissions("baseData:list")
	@RequestMapping("/list")
	@ResponseBody
	public PageInfo<BaseData> list(@RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "10") Integer pageSize, String keyword) {
		System.out.println("keyword :" + keyword);
		// 开始分页
		PageHelper.startPage(pageNum, pageSize);

		BaseDataExample example = new BaseDataExample();

		// 关键字不为空才做条件查询
		if (StringUtils.isNotBlank(keyword)) {
			// 创建限制对象
			Criteria criteria1 = example.createCriteria();
			criteria1.andBaseNameLike("%" + keyword + "%");
			
			//根据所属类别查询
			Criteria criteria2 = example.createCriteria();
			criteria2.andParentNameLike("%" + keyword + "%");
			
			example.or(criteria2);
		}

		List<BaseData> baseDatas = baseDataService.selectByExample(example);

		// 创建分页信息对象
		return new PageInfo<BaseData>(baseDatas);

	}

	// 删除功能
	@RequestMapping("/insert")
	@ResponseBody
	public MsgObject insert(BaseData baseData) {

		int row = baseDataService.insert(baseData);
		MsgObject mo = MsgObject.createMO(0, "新增数据失败，请联系管理员！！");
		if (row == 1) {
			mo = MsgObject.createMO(1, "新增数据成功!");
		}
		return mo;
	}

	// 删除功能
	@RequestMapping("/update")
	@ResponseBody
	public MsgObject update(BaseData baseData) {

		int row = baseDataService.updateByPrimaryKeySelective(baseData);
		MsgObject mo = MsgObject.createMO(0, "修改数据失败，请联系管理员！！");
		if (row == 1) {
			mo = MsgObject.createMO(1, "修改数据成功!");
		}
		return mo;
	}

	// 删除功能
	@RequestMapping("/delete")
	@ResponseBody
	public MsgObject delete(Long baseId) {
		/*
		 * 
		 * 判断要删除数据在t_base_data 表中是否还有parent_id为删除的id的值
		 * 有：说明有子数据，不能删除
		 * 没有：可以删除
		 * 
		 */
		
		BaseDataExample example = new BaseDataExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(baseId);
		List<BaseData> children = baseDataService.selectByExample(example );
		
		MsgObject mo = MsgObject.createMO(0, "删除数据失败，请联系管理员！！");
		
		if(children.size() > 0) {
			mo = MsgObject.createMO(0, "此数据还有子数据，不能删除！！");
			return mo;
		}

		int row = baseDataService.deleteByPrimaryKey(baseId);
		if (row == 1) {
			mo = MsgObject.createMO(1, "删除数据成功!");
		}
		return mo;
	}

}
